﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FlyBird
{
    class Barrier
    {
        //矩形对象
        private Rectangle rect;

        //是否可移动，是否下移，true表示下移，false表示上移
        private bool mob = true;

        //障碍物的速度
        private int speed = 3;
        //障碍物需要的三个图片
        private static Image[] imgs;
        //存活状态
        private bool visible;
        //障碍物的初始高度，上下障碍可以伸长缩短，需要初始高度
        private int initHeight;

 


        static Barrier()
        {
            //图片三种，上中下
            int COUNT = 3;
            imgs = new Image[COUNT];
            for (int i = 0; i < COUNT; i++)
            {
                imgs[i] = GameUtil.loadBufferedImage(StaticValue.BARRIER_IMG_PATH[i]);
            }
            //赋值
            BARRIER_WIDTH = imgs[0].Width; //中间宽度
            BARRIER_HEIGHT = imgs[0].Height; //中间高度
            BARRIER_HEAD_WIDTH = imgs[1].Width; //头部宽度
            BARRIER_HEAD_HEIGHT = imgs[1].Height; //头部高度
        }
        //位置
        private int x, y;


        //当前宽度和高度
        private int width, height;

        //障碍物类型
        private int type;


        public const int TYPE_TOP_NORMAL = 0;//头部不可动
        public const int TYPE_BOTTOM_NORMAL = 2;//底部不可动
        public const int TYPE_HOVER_NORMAL = 4;//中间不可动
        public const int TYPE_MOVE_TOP_NORMAL = 6;//头部可移动
        public const int TYPE_MOVE_BOTTOM_NORMAL = 8;//底部可移动 
        public const int TYPE_MOVE_HOVER_NORMAL = 10;//中间可移动
        

        //获取障碍物的高度和宽度,有三种，上下两种一样的
        public static readonly int BARRIER_WIDTH;
        public static readonly int BARRIER_HEIGHT;
        public static readonly int BARRIER_HEAD_WIDTH;
        public static readonly int BARRIER_HEAD_HEIGHT;

        public Barrier() {
            rect = new Rectangle();
        }

        public Barrier(int x, int y, int height, int type)
        {
            this.x = x;
            this.y = y;
            this.height = height;
            this.type = type;
            this.width = BARRIER_WIDTH;
        }
        //根据不同的类型绘制障碍物
        public void draw(Graphics g)
        {
            switch (type)
            { 
                case TYPE_TOP_NORMAL:
                    drawTopNormal(g);
                    break;
                case TYPE_BOTTOM_NORMAL:
                    drawNormalTop(g);
                    break;
                case TYPE_HOVER_NORMAL:
                    drawHoverNormal(g);
                    break;
                case TYPE_MOVE_TOP_NORMAL:
                    drawMoveTopNormal(g);
                    break;
                case TYPE_MOVE_BOTTOM_NORMAL:
                    drawMoveBottomNormal(g);
                    break;
                case TYPE_MOVE_HOVER_NORMAL:
                    drawMoveHoverNormal(g);
                    break;
            }
        }

        //绘制从上往下的障碍物
        private void drawTopNormal(Graphics g)
        {
            //求出所需的中间障碍物的数量，（高度-头部高度） / 中间高度 + 1
            int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT + 1;
            //循环绘中间的障碍物
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x, y + i * BARRIER_HEIGHT);
            }
            //绘制头
            int tempY = height - BARRIER_HEAD_HEIGHT;
            g.DrawImage(imgs[2], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, tempY);
            //向左移动
            x -= speed;
            //移出屏幕失效
            if (x < -50)
            {
                visible = false;
            }
            //绘制矩形对象
            drawRect(g);
        }
        //绘制从下往上
        private void drawNormalTop(Graphics g)
        {
            //求出所需的中间障碍物的数量
            int count = height / BARRIER_HEIGHT + 1;
            //循环绘中间制障碍物
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x, StaticValue.FRAM_HEIGHT - i * BARRIER_HEIGHT);
            }
            //绘制头
            int tempY = StaticValue.FRAM_HEIGHT - height;
            g.DrawImage(imgs[1], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, tempY);
            x -= speed;
            if (x < -50)
            {
                visible = false;
            }
            drawRect(g);
        }

        //绘制中间障碍物
        public void drawHoverNormal(Graphics g)
        {
            int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
            //绘制上头
            g.DrawImage(imgs[1], x, y);
            //绘制中间
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x + (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, y + BARRIER_HEAD_HEIGHT + i * BARRIER_HEIGHT);
            }
            //绘制矩形对象
            drawRect(g);
            //绘制下头
            int tempY = y + height - BARRIER_HEAD_HEIGHT;
            g.DrawImage(imgs[2], x, tempY);
            x -= speed;
            if (x < -50)
            {
                visible = false;
            }

        }
        //绘制顶部可以移动障碍物
        public void drawMoveTopNormal(Graphics g)
        {
            //求出所需的中间障碍物的数量，（高度-头部高度） / 中间高度 + 1
            int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT + 1;
            //循环绘中间的障碍物
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x, y + i * BARRIER_HEIGHT);
            }
            //绘制头
            int tempY = height - BARRIER_HEAD_HEIGHT;
            g.DrawImage(imgs[2], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, tempY);
            drawRect(g);
            //向左移动
            x -= speed;
            //移出屏幕失效
            if (x < -50)
            {
                visible = false;
            }
            //高度在初始高度上下20单位波动
            if (mob)
            {
                height += 2;
                if (height >= initHeight + 20)
                {
                    mob = false;
                }
            }
            else
            {
                height -= 2;
                if (height <= initHeight - 20)
                {
                    mob = true;
                }
            }
        }

        //绘制底部可以移动障碍物
        public void drawMoveBottomNormal(Graphics g)
        {
            //求出所需的中间障碍物的数量
            int count = height / BARRIER_HEIGHT + 1;
            //循环绘中间制障碍物
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x, StaticValue.FRAM_HEIGHT - i * BARRIER_HEIGHT);
            }
            //绘制头
            int tempY = StaticValue.FRAM_HEIGHT - height;
            g.DrawImage(imgs[1], x - (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, tempY);
            drawRect(g);
            x -= speed;
            if (x < -50)
            {
                visible = false;
            }
            //高度在初始高度上下20单位波动
            if (mob)
            {
                height += 2;
                if (height >= initHeight + 20)
                {
                    mob = false;
                }
            }
            else
            {
                height -= 2;
                if (height <= initHeight - 20)
                {
                    mob = true;
                }
            }

        }

        //绘制中间可移动障碍物
        public void drawMoveHoverNormal(Graphics g)
        {
            int count = (height - BARRIER_HEAD_HEIGHT) / BARRIER_HEIGHT;
            //绘制上头
            g.DrawImage(imgs[1], x, y);
            for (int i = 0; i < count; i++)
            {
                g.DrawImage(imgs[0], x + (BARRIER_HEAD_WIDTH - BARRIER_WIDTH) / 2, y + BARRIER_HEAD_HEIGHT + i * BARRIER_HEIGHT);
            }
            drawRect(g);
            //绘制下头
            int tempY = y + height - BARRIER_HEAD_HEIGHT;
            g.DrawImage(imgs[2], x, tempY);
            x -= speed;
            if (x < -50)
            {
                visible = false;
            }
            if (mob)
            {
                y += 2;
                if (y >= 300)
                {
                    mob = false;
                }
            }
            else
            {
                y -= 2;
                if (y <= 0)
                {
                    mob = true;
                }
            }
        }

        //什么时候绘制下一组障碍物，通常拿最新的障碍物来判断，判断最新障碍物是否进入屏幕
        public bool isInFrame()
        {
            return 600 - x > 250; 
        }

        
        //绘制障碍物的碰撞矩形
        public void drawRect(Graphics g)
        {
            int x1 = this.x;
            int y1 = this.y;
            int w1 = imgs[0].Width;
            //Pen pen = new Pen(Color.Blue);
            setRectangle(x1, y1, w1, height);
            //g.DrawRectangle(pen, rect);
        }

        //障碍物矩形参数设置
        public void setRectangle(int x, int y, int width, int height)
        {
            rect.X = x;
            rect.Y = y;
            rect.Width = width;
            rect.Height = height;
        }

        public int _y
        {
            get { return y; }
            set { y = value; }
        }

        public int _x
        {
            get { return x; }
            set { x = value; }
        }

        public int _initHeight
        {
            get { return initHeight; }
            set { initHeight = value; }
        }

        public int _height
        {
            get { return height; }
            set { height = value; }
        }

        public int _type
        {
            get { return type; }
            set { type = value; }
        }


        public bool _visible
        {
            get { return visible; }
            set { visible = value; }
        }


        public Rectangle _rect
        {
            get { return rect; }
            set { rect = value; }
        }
    }
}
